﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MM.Commmon.UI;
using MM.Commmon.UI.Controls;
using System.ComponentModel;
using System.Windows.Forms;
using DevExpress.XtraGrid.Columns;
using System.Data;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraEditors.Repository;
using MM.Common.DBBase;
using MM.Common.Base;
using MM.Common.Setup;
using DevExpress.XtraEditors;
using System.Drawing;
using System.Drawing.Drawing2D;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraGrid.Views.Base;

namespace MM.Common.Support
{
    public partial class FrmEmpMenuSetup : FrmBase
    {
        private DataTable dtMenu;

        private void FrmZgxxMenu_Load(object sender, EventArgs e)
        {
        }

        public FrmEmpMenuSetup(DataTable dtmenu)
        {
            this.InitializeComponent();
            this.dtMenu = dtmenu;
            this.gvMenu.GridControl.DataSource = this.dtMenu;
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (this.gvMenu.FocusedRowHandle >= 0)
            {
                this.gvMenu.DeleteRow(this.gvMenu.FocusedRowHandle);
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (this.gvMenu.FocusedRowHandle >= 0)
            {
                this.dgvSysMenu.Rows.Clear();
                string paramValue = this.gvMenu.GetFocusedRowCellValue("SYSTEM_CODE").ToString();
                string commandtext = "SELECT ORDER_NO ,TITLE FROM MXHLY_SYS_MENU WHERE SYSTEM_CODE = :sscode AND FUNCTION_CODE IS NOT NULL ORDER BY ORDER_NO";
                DataParamCol parm = new DataParamCol(new DataParam[] { new InputDataParam("sscode", paramValue) });
                DataTable dt = new DataTable();
                SystemInfo.DbData.Retrieve(commandtext, parm, ref dt);
                int[] numArray = new int[2];
                numArray[1] = 100;
                FrmSelectFromGrid grid = new FrmSelectFromGrid(dt, new string[] { "序号", "菜单" }, numArray, "菜单选择");
                if (grid.ShowDialog(this) != DialogResult.Cancel)
                {
                    int choiceRow = grid.ChoiceRow;
                    this.gvMenu.SetFocusedRowCellValue("ORDER_NO", dt.Rows[choiceRow]["ORDER_NO"]);
                    this.gvMenu.SetFocusedRowCellValue("TITLE", dt.Rows[choiceRow]["TITLE"]);
                    grid.Dispose();
                }
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private void btnInsert_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            SystemInfo.DbData.Retrieve("SELECT SYSTEM_CODE ,SYSTEM_NAME FROM MXHLY_SYS_SYSTEM ORDER BY SYSTEM_CODE", ref dt);
            int[] numArray = new int[2];
            numArray[1] = 100;
            FrmSelectFromGrid grid = new FrmSelectFromGrid(dt, new string[] { "代码", "名称" }, numArray, "子系统选择");
            if (grid.ShowDialog(this) != DialogResult.Cancel)
            {
                int choiceRow = grid.ChoiceRow;
                if (this.dtMenu.Select(string.Format("SYSTEM_CODE='{0}'", dt.Rows[choiceRow]["SYSTEM_CODE"])).Length > 0)
                {
                    XtraMessageBox.Show("该子系统已经存在,无法添加!");
                }
                else
                {
                    DataRow row = this.dtMenu.NewRow();
                    row["SYSTEM_CODE"] = dt.Rows[choiceRow]["SYSTEM_CODE"].ToString();
                    row["SYSTEM_NAME"] = dt.Rows[choiceRow]["SYSTEM_NAME"].ToString();
                    this.dtMenu.Rows.Add(row);
                    this.btnEdit_Click(sender, e);
                }
                grid.Dispose();
            }
        }
    }
}
